﻿using Furion.DependencyInjection;
using FaceMqttClient.model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FaceMqttClient
{
    public class DeviceCheck : ITransient
    {
        private readonly ISugarQueryable<devevent> devevent;
        private readonly ISqlSugarClient db;

        public DeviceCheck(ISqlSugarClient db)
        {
            this.devevent = db.Queryable<devevent>();
            this.db = db;
        }

        public List<devevent> Getdevevent(int state)
        {
            return devevent.Context.Queryable<devevent>()
                .GroupBy(d => d.school)
                .Where(d => d.wstz == 0 && d.state == state)
                .Select(d => new devevent
                {
                    ctime = SqlFunc.AggregateMin(d.ctime),
                    school = d.school,
                    state = d.state,
                    id=SqlFunc.AggregateMax(d.id),
                    name = SqlFunc.MappingColumn(default(string), "GROUP_CONCAT(name)")
                })
                .ToList();
        }

        public void delesend(devevent dev)
        {
           // 推荐操作
            db.Deleteable<devevent>().Where(it => it.school == dev.school && it.state==dev.state && it.id<= dev.id).ExecuteCommand();
        }
        public void sended(devevent dev)
        {
            dev.wstz++;

            db.Updateable<devevent>(dev).UpdateColumns(new string[] { "wstz" }).Where(d => d.id == dev.id).ExecuteCommandAsync();
        }
    }
}
